﻿@model Dyd.BaseService.ConfigManager.Domain.Model.tb_project_model
@using Dyd.BaseService.ConfigManager.Domain.Model
@{
    ViewBag.Title = "新增项目";
    Layout = "~/Views/Shared/_Layout.cshtml";
    IList<tb_category_model> categoryList = ViewBag.CategoryList as IList<tb_category_model>;
}
<div class="head">
    <div class="top">
        <h3 class="fl">@ViewBag.Title</h3>
        <a href="javascript:void(0);" class="back fr">返回</a>
    </div>
</div>
<div>
    @using (Html.BeginForm("ProjectAdd", "Project"))
    {
        @Html.AntiForgeryToken()
        <ul class="mydetail">
            <li>
                @Html.LabelFor(model => model.projectname)
                @Html.EditorFor(model => model.projectname)
                @Html.ValidationMessageFor(model => model.projectname)
            </li>
            <li>
                @Html.LabelFor(model => model.remark)
                @Html.TextAreaFor(model => model.remark, new { @rows=5})
                @Html.ValidationMessageFor(model => model.remark)
            </li>
            <li>
                <label>配置分类</label>
                <label><span style="color:red"> 如果存在相同配置，下面配置项覆盖上面配置项</span></label>
                <div style="width:600px;margin-left:185px;">

                    <label></label>
                    <div class="insertCategory">
                    </div>
                </div>
            </li>
            <li>
                <div>
                    <span class="searchText">分类名</span><input id="categoryName" placeholder="分类名" type="text" />
                    <span class="searchText">备注</span><input id="categoryRemark" placeholder="备注" type="text" />

                    <input id="btnsearch" type="button" class="btn1" value="查找" />
                </div>
                <table id="list" style="width:50%"></table>
            </li>
            <li>
                <input id="save" type="button" class="btn1" value="提交并通知相关客户端重启" />
                @Html.ValidationMessage("Error")
            </li>
        </ul>
    }
</div>
@section Scripts{
    <script src="~/Content/easyui/jquery.easyui.min.js"></script>
    <link href="~/Content/easyui/themes/default/easyui.css" rel="stylesheet" />
    <script src="~/Content/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script>
        $(function () {

            $("#list").datagrid({
                url: '/Category/GetPagingCategoryList',
                width: $(window).width() * 0.8,
                methord: "post",
                height: 460,
                fitColumns: true,
                idField: "Id",
                pageSize: 15,
                pageList: [5, 10, 15, 20, 25, 30],
                pagination: true,
                striped: true,
                singleSelect: true,
                rownumbers: true,
                autoRowHeight: false,
                rowStyler: function (index, row) { return "height:25px;"; },
                columns: [[
                    { field: "Id", title: "Id", width: 120, hidden: true },
                    { field: "name", title: "配置名称", width: 120, sortable: true },
                    { field: "remark", title: "备注", width: 120, sortable: true }
                ]],
                onDblClickRow: function (index, row) {
                    var id = row.id;
                    var name = row.name;
                    var ids = GetCategoryIds();
                    if (ids.indexOf("," + id + ",") < 0) {
                        var routeValuesHtml = "<div style='font-size:20pt;line-height:30px;padding:1px;' class='categoryRow'><span style='display:block;float:left;'>↓</span><input class='categoryId' type='hidden' value='" + id + "' /><span style='width:300px;height:30px; background-color:#98d0f5; display:block;float:left;'>" + name + "</span><img class='moveUp' style='width:16px;height:16px;' src='/Content/images/gtk_go_up.png'><img class='erase' style='width:16px;height:16px;' src='/Content/images/erase.png'></div>";
                        $(".insertCategory").append(routeValuesHtml);
                    }
                }
            });

            $("#btnsearch").on("click", function () {
                var txtName = $("#categoryName").val();
                var texRemark = $("#categoryRemark").val();
                if (txtName == null) {
                    txtName = "";
                }
                if (texRemark == null) {
                    texRemark = "";
                }
                $("#list").datagrid("load", {
                    name: txtName,
                    remark: texRemark
                });
            });

            $("body").on("click", ".moveUp", function () {
                var insertBefore = $(this).parent().prev();
                $(this).parent().insertBefore(insertBefore);
            });

            $("body").on("click", ".erase", function () {
                $(this).parent().remove();
            });

            $("#save").click(function () {
                if (!$("form").valid()) {
                    return;
                }
                var ids = GetCategoryIds();
                var submitJson = $("form").serialize();
                $.ajax({
                    url: "/Project/ProjectAdd",
                    data: submitJson + "&categoryids=" + ids,
                    type: "post",
                    success: function (data) {
                        if (data.Flag) {
                            alert(data.Message);
                            window.location.href = "/Project/ProjectIndex";
                        } else {
                            alert(data.Message);
                        }
                    }
                });
            });

            $("body").on("click", ".del", function (e) {
                var id = $(this).parent().parent().attr("data-id");
                var url = "/Category/CategoryDel";
                if (window.confirm("确定要删除吗？")) {
                    $.post(url, addAntiForgeryToken({ id: id }), function (data) {
                        if (data.Flag) {
                            alert(data.Message);
                            window.location.reload();
                        }
                        else {
                            alert(data.Message);
                        }
                    }, "json");
                }
            });
        });

        function GetCategoryIds() {
            var ids = ",";
            $(".categoryRow").each(function (j) {
                ids += $.trim($(this).children(".categoryId").val()) + ",";
            });
            return ids;
        }

    </script>
}